

package com.hazelcast.multimap.impl;

import java.util.*;

/**
 * The MultiMapValue is the value in a multimap, and it contains either a set or a list as collection.
 */
public class MultiMapValue {

    private final Collection<MultiMapRecord> collection;

    private long hits;

    public MultiMapValue(Collection<MultiMapRecord> collection) {
        this.collection = collection;
    }

    public Collection<MultiMapRecord> getCollection(boolean copyOf) {
        if (copyOf) {
            return getCopyOfCollection();
        }
        return collection;
    }

    private Collection<MultiMapRecord> getCopyOfCollection() {
        if (collection instanceof Set) {
            return new HashSet<>(collection);
        } else
            if (collection instanceof List) {
                return new LinkedList<>(collection);
            }
        throw new IllegalArgumentException("No Matching CollectionProxyType!");
    }

    public void incrementHit() {
        hits++;
    }

    public long getHits() {
        return hits;
    }

    public void setHits(long hits) {
        this.hits = hits;
    }

    public boolean containsRecordId(long recordId) {
        for (MultiMapRecord multiMapRecord : collection) {
            if (multiMapRecord.getRecordId() == recordId) {
                return true;
            }
        }
        return false;
    }
}
